package com.chinawlx.wlxteacher.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.widget.radiogroup.WLXFlowRadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * Created by wlx on 16/8/26.
 */
public class WLXOpinionActivity extends WLXBaseActivity{
    @BindView(R.id.et_opinion)
    EditText opinionEdit;
    @BindView(R.id.btn_submit)
    Button submitBtn;
    @BindView(R.id.flowRadioGroup)
    WLXFlowRadioGroup mFlowRadioGroup;
    @BindView(R.id.iv_back_opinion)
    ImageView backBtn;
    private RadioButton[] radioButtons;
    private String[] titles = {"软件错误", "信息不符", "反应迟钝", "点击不灵", "使用问题"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opinion);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initDate();
        initView();
    }
    private String checkText;
    private void initView() {
        mFlowRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < radioButtons.length; i++) {
                    if (radioButtons[i].getId() == checkedId) {
                        //得到选中的文字
                        checkText = radioButtons[i].getText().toString();
                        Toast.makeText(WLXOpinionActivity.this, checkText, Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }
        });
        //提交
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = opinionEdit.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(WLXOpinionActivity.this, "请填写反馈信息", Toast.LENGTH_SHORT)
                            .show();
                }else {
                    sendRequestForNet(content,checkText);
                }
            }


        });
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void sendRequestForNet(String content,String checkText) {
        Toast.makeText(WLXOpinionActivity.this, "提交成功", Toast.LENGTH_SHORT)
                .show();
    }

    private void initDate() {
        initRadioButton();
    }

    /**
     * 动态添加内容
     */
    private void initRadioButton() {
        WindowManager manager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
        int width = manager.getDefaultDisplay().getWidth();
        // 动态加载radiobutton的时候要用  radioGroup.layoutParams 来实现
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(width / 3 - 65,
                WRAP_CONTENT);
        layoutParams.setMargins(18, 13, 18, 10);
        radioButtons = new RadioButton[titles.length];
        for (int i = 0; i < radioButtons.length; i++) {
            radioButtons[i] = new RadioButton(this);
            radioButtons[i].setText(titles[i]);
            radioButtons[i].setButtonDrawable(null);
            radioButtons[i].setBackgroundResource(R.drawable.selector_feedback);
            radioButtons[i].setTextColor(this.getResources().getColorStateList(R.color
                    .color_feedback_name));
            radioButtons[i].setGravity(Gravity.CENTER);
            radioButtons[i].setTag(i);
            radioButtons[i].setId(i);
            radioButtons[i].setLayoutParams(layoutParams);
            mFlowRadioGroup.addView(radioButtons[i]);
        }
    }
}

